#include <stdio.h>
#include <malloc.h>
#include <string.h>
int n;
//预定义数据结构
typedef struct stuInfo {
    char stuName[10]; /*学生姓名*/
    int Age; /*年龄*/
} ElemType;
typedef struct node {
    ElemType data;
    struct node* next;
} ListNode, * ListPtr;
ListPtr CreateList()
{
    int i, len;
    //开辟头结点
    ListPtr p_head = (ListPtr)malloc(sizeof(ListNode));
    ListPtr p_tail = p_head;
    p_tail->next = NULL;
    printf("请输入节点的个数:");
    scanf("%d", &len);
    for (i = 0; i < len; i++) {
        ListPtr pNew = (ListPtr)malloc(sizeof(ListNode));//开辟新节点
        printf("请输入学生姓名:");
        scanf("%s",pNew->data.stuName);
        printf("请输入年龄:");
        scanf("%d", &pNew->data.Age);
        p_tail->next = pNew;//将尾节点指向新节点
        pNew->next = NULL;//新节点应该指向NULL
        p_tail = pNew;//当前尾节点为新节点
    }
    return p_head;
}
void PrintList(ListPtr p)
{
    ListPtr t = p;
    if (p == NULL) {
        printf("链表为空\n");
        return;
    }
    while (t->next)
    {
        t = t->next;
        printf("%s", "学生姓名:");
        printf("%s", t->data.stuName);
        printf("年龄: %d", t->data.Age);
        printf("\n");
    }
}
void InsertList(ListPtr p) {
    ListPtr t = p;
    if (t == NULL)
    {
        printf("链表为空\n");
        return;
    }
    while (t->next)
    {
        t = t->next;
    }
    ListPtr q = (ListPtr)malloc(sizeof(ListNode));
    printf("请输入学生姓名:");
    scanf("%s",q->data.stuName);
    printf("请输入年龄:");
    scanf("%d", &q->data.Age);
    q->next = t->next;
    t->next = q;
}
int main(int argc, char argv[])
{
    ListPtr listHead = NULL;
    char command = '0';
    while (1)
    {
        printf("1 Create List\n");
        printf("2 Printf List\n");
        printf("3 Insert List\n");
        printf("4 Quit\n");
        printf("请选择：");
        char temp = getchar();
        if (temp != '\n') {
            command = temp;
        }
        else {
            command = getchar();
        }
        switch (command)
        {
        case '1':listHead = CreateList();
                 break;
        case '2':PrintList(listHead);
                 break;
        case '3':InsertList(listHead);
                 break;
        case '4': return 0;
        default:printf("error \n");
            break;
        }   
    }
    return 0;
}
